﻿using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System;
using System.Diagnostics;

namespace wicpowerTcpTool
{
    public class TempMessageAdorner : Adorner
    {
     

        private UIElement _child;
        private double _offsetX;
        private double _offsetY;

        public TempMessageAdorner(UIElement adornedElement, UIElement child, double offsetX, double offsetY)
            : base(adornedElement)
        {
            _child = child;
            _offsetX = offsetX;
            _offsetY = offsetY;
            AddVisualChild(_child);
        }

        protected override int VisualChildrenCount => 1;

        protected override Visual GetVisualChild(int index)
        {
            if (index == 0) return _child;
            throw new ArgumentOutOfRangeException();
        }

        protected override Size MeasureOverride(Size constraint)
        {
            _child.Measure(constraint);
            return _child.DesiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
        

            var desired = _child.DesiredSize;

            // 用 AdornedElement 的实际宽度和高度来计算位置
            double parentWidth = ((FrameworkElement)this.AdornedElement).ActualWidth;
            double parentHeight = ((FrameworkElement)this.AdornedElement).ActualHeight;

            double x = parentWidth - desired.Width - _offsetX;
            double y = parentHeight - desired.Height - _offsetY;

            _child.Arrange(new Rect(new Point(x, y), desired));
            Debug.WriteLine($"AdornedElement ActualWidth={parentWidth}, ActualHeight={parentHeight}");
            return finalSize;
        }

    }
}
